<template>
  <div>
    <el-card>
      <el-table :data="taskList" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="任务 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="任务名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="任务类型">
                <span>{{ props.row.typeClassify == 1 ? '日常任务' : '一次性任务' }}</span>
              </el-form-item>
              <el-form-item label="任务介绍">
                <span>{{ props.row.introduce }}</span>
              </el-form-item>
              <el-form-item label="确认方式">
                <span>{{ props.row.accomplishType == 1 ? '主账号确认' : '次级账号确认' }}</span>
              </el-form-item>
              <el-form-item label="需求等级">
                <span>{{ props.row.prepositionLevel }}</span>
              </el-form-item>
              <el-form-item label="经验奖励">
                <span>{{ props.row.awardLevel }}</span>
              </el-form-item>
              <el-form-item label="金币奖励">
                <span>{{ props.row.awardMoney }}</span>
              </el-form-item>
              <el-form-item label="经验惩罚">
                <span>{{ props.row.punishmentLevel }}</span>
              </el-form-item>
              <el-form-item label="金币惩罚">
                <span>{{ props.row.punishmentMoney }}</span>
              </el-form-item>
              <el-form-item label="任务限时" v-if="props.row.limitTime != null">
                <span>{{ props.row.limitTime }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.createTime }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="任务 ID" prop="id"></el-table-column>
        <el-table-column label="任务名称" prop="name"></el-table-column>
        <el-table-column label="任务类型">
          <template slot-scope="pro">
            <span>{{ pro.row.typeClassify == 1 ? '日常任务' : '一次性任务' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="pro">
            <el-button style="float:left;" v-if="pro.row.accomplishType == 2" type="primary" @click="accomplishTask(pro.row.utId, pro.row.id, 1)"
              :disabled="finishTaskDisabled(pro.row.accomplishType)" width="15px">完成任务</el-button>
            <el-button style="float:left" v-if="pro.row.accomplishType == 1" type="primary" @click="accomplishTask(pro.row.utId, pro.row.id, 3)">提交审核</el-button>

            <el-button style="float:left" type="warning" @click="accomplishTask(pro.row.utId, pro.row.id, 2)"
              :disabled="isReceive(pro.row.isReceive)" width="15px">放弃任务</el-button>
              <!-- 提交文件 要改这个上传地址-->
            <el-upload  action="none" :limit="1" :auto-upload="true" :http-request="uploadFile"
              :on-exceed="handleExceed" :file-list="fileList" :data="pro.row.utId">
              <el-button style="float:left;margin-left:8px" type="primary" width="15px">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-sizes="[2, 5, 10]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </el-card>
  </div>
</template>


<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import {
  getSecondaryOngoingTask,
  secondaryAccomplishTask,
  uploadImg
} from "@/utils/SecondaryApi";
export default {
  data() {
    return {
      map: new Map,
      formData:"",
      fileList: [],
      queryInfo: {
        query: "",
        // 当前页数
        pageNum: 1,
        // 每页显示多少数据
        pageSize: 10,
      },
      taskList: [],
      total: 0,
    };
  },
  created() {
    this.getTaskList();
  },
  methods: {
    async uploadFile (file) {
      this.formData = new FormData();
      this.formData.append("file", file.file);
      console.log( file);

      const { data: res } = await uploadImg(this.formData);
      if (res.meta.status !== 200) {
        return this.$message.error("上传失败");
      }
      console.log(file.data)
      console.log(res.data)
      this.map.set(file.data,res.data);
    },
    handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
    async getTaskList() {
      const { data: res } = await getSecondaryOngoingTask(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取任务列表失败");
      }
      this.taskList = res.data.list;
      this.total = res.data.total;
    },
    async accomplishTask(utId, taskId, isAccomplish) {
      console.log(utId)
      const { data: res } = await secondaryAccomplishTask({
        id: utId,
        taskId: taskId,
        isAccomplish: isAccomplish,
        filePath: this.map.get(utId)
      });
      if (res.meta.status !== 200) {
        return this.$message.error("完成任务请求失败");
      } else {
        this.$message({
          message: "任务已完成",
          type: "success",
        });
      }
      this.getTaskList();
      setTimeout(() => {
        this.$store.commit("getUserInfo");
      }, 5000);
    },
    isReceive(flag) {
      return flag == 1;
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getTaskList();
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageNum = newSize;
      this.getTaskList();
    },
    finishTaskDisabled(flag) {
      return flag === 1;
    },
  },
};
</script>